package com.eduadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.eduadmin.entity.Exam;
import com.eduadmin.entity.Grade;
import com.eduadmin.entity.Page;
import com.eduadmin.listener.GradeDataUploadListener;
import com.eduadmin.service.IGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GradeController {
    //构造函数自动装配
    private final IGradeService gradeService;
    @Autowired
    public GradeController(IGradeService gradeService){
        this.gradeService= gradeService;
    }
    //新增
    @RequestMapping(value = "newAddGrade")
    @ResponseBody
    public Map<String, String> newAddGrade(Grade grade, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        Grade gd = gradeService.getGradeByGradeName(grade.getGradeName());
        if (gd != null) {
            //已经在数据库中
            ret.put("type", "error");
            ret.put("msg", "增加失败，该名称已经在系统中！");
            return ret;
        }
        int n = gradeService.newAddGrade(grade);
        if (1 != n) {
            //新增失败
            ret.put("type", "error");
            ret.put("msg", "增加失败！");
        } else {
            //新增成功 更新session
            List<Grade> grades = gradeService.getAllGrades();
            request.getSession().setAttribute("grades", grades);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        }
        return ret;
    }
   
    //编辑 
    @RequestMapping(value = "editGrade")
    @ResponseBody
    public Map<String, String> editGrade(Grade grade, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        Grade ga = gradeService.getGradeByGradeName(grade.getGradeName());
        if (ga == null) {
            //实际上执行的插入操作
            gradeService.newAddGrade(grade);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        } else {
            //更新
            gradeService.updateGrade(grade);
            ret.put("type", "success");
            ret.put("msg", "修改成功！");
        }
        List<Grade> grades = gradeService.getAllGrades();
        request.getSession().setAttribute("grades", grades);
        return ret;
    }

    //删除 
    @RequestMapping(value = "deleteGrade")
    @ResponseBody
    public Map<String, String> deleteGrade(@RequestParam(value = "ids[]") String[] idArray,HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < idArray.length; i++) {
            sb.append("'" + idArray[i] + "'");
            if (i < idArray.length - 1) {
                sb.append(",");
            }
        }
        String idString = sb.toString();
        Map<String, String> ret = new HashMap<>();
        int n = gradeService.deleteGrade(idString);
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除成功！");
            List<Grade> grades=gradeService.getAllGrades();
            request.getSession().setAttribute("grades",grades);
        } else {
            ret.put("type", "error");
            ret.put("msg", "删除失败！");
        }
        return ret;
    }

    
    //导入年级数据
    @RequestMapping(value = "addGrades")
    @ResponseBody
    public String addGrades(MultipartFile gradesFile) throws IOException {
        EasyExcel.read(gradesFile.getInputStream(), Grade.class, new GradeDataUploadListener(gradeService)).sheet().doRead();
        return "上传成功";
    }
    //获取所有
    @RequestMapping(value = "getAllGrades")
    @ResponseBody
    public Map<String, Object> getAllGrades(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows
    ) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());

        List<Grade> gradeList = gradeService.getAllGradeList(paraMap);
        Long total = gradeService.getAllTotal(paraMap);
        reMap.put("rows", gradeList);
        reMap.put("total", total);

        return reMap;
    }

}
